package edu.bit.water.service;


import edu.bit.water.dao.MetaTIDao;
import edu.bit.water.entity.MetaAI;
import edu.bit.water.entity.TechnicalIndicator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import java.util.*;

import edu.bit.water.entity.MetaTI;

/**
 * meta_assessment_indicator数据表对应的service类
 * @Author: Page
 */
@Service
public class MetaTIService {

    @Autowired
    private MetaTIDao MetaTIDao;

    public List<MetaTI> selectMetaTI(String stNumber) {
        return MetaTIDao.selectMetaTI(stNumber);
    }
    public List<MetaTI> selectMetaTINote(String stNumber) {
        return MetaTIDao.selectMetaTINote(stNumber);
    }
    public Map<String, Object> insertMetaTINote(MetaTI metaTI) {
        Map<String, Object> result = new HashMap<>();
        Integer id = MetaTIDao.getMaxId1();
        if (id != null) {
            metaTI.setId(id + 1);
        }
        int tmp = MetaTIDao.insertMetaTINote(metaTI);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> deleteMetaTINote(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            int tmp = MetaTIDao.deleteMetaTINote(Integer.parseInt(ids.get(i)));
            if (tmp == 1) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> updateMetaTINote(MetaTI metaTI) {
        Map<String, Object> result = new HashMap<>();
        int tmp = MetaTIDao.updateMetaTINote(metaTI);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> insertMetaTI(MetaTI metaTI) {
        Map<String, Object> result = new HashMap<>();
        Integer id = MetaTIDao.getMaxId();
        if (id != null) {
            metaTI.setId(id + 1);
        }
        int tmp = MetaTIDao.insertMetaTI(metaTI);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> deleteMetaTI(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            int tmp = MetaTIDao.deleteMetaTI(Integer.parseInt(ids.get(i)));
            if (tmp == 1) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    //级联删除technical_indicator_table
    public Map<String, Object> technicalDelete(List<String> tempData) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        String standardNumber = tempData.get(tempData.size() -1);//最后一个数据是标准号
        for (int i = 0; i < tempData.size() - 1; i++) {
            int tmp = MetaTIDao.technicalDelete(tempData.get(i).toString(), standardNumber);
            if (tmp == 1) {
                result.put(tempData.get(i).toString(), "success");
            }
            else {
                result.put(tempData.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> updateMetaTI(MetaTI MetaTI) {
        Map<String, Object> result = new HashMap<>();
        int tmp = MetaTIDao.updateMetaTI(MetaTI);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    /**
     * 级联编辑technical_indicator_table
     * author Page
     */
    public Map<String, Object> technicalUpdate(TechnicalIndicator technicalIndicator) {
        Map<String, Object> result = new HashMap<>();
        int tmp = MetaTIDao.technicalUpdate(technicalIndicator);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
}